
/**
 * 验证是否为电话号码（座机）
 *
 * @param {}
 *            source
 */

function isTelephone(source) {
    var regex = /^(0[0-9]{2,3}\-)?([2-9][0-9]{6,7})+(\-[0-9]{1,4})?$/
    return regex.test(source);  //search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1
}

/**
 * 验证是否为手机号码（移动手机）
 *
 * @param {}
 *            source
 */

function isMobilePhone(source) {

    return /^1[345678]\d{9}$/.test(source)
    // var regex = /^((\(\d{3}\))|(\d{3}\-))?1\d{10}/;
    // return regex.test(source);
}

function isEmail(source) {
    var regex = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
    if (source.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1) {
        return true;
    } else {
        alert("电子邮箱格式不正确");
        return false;
    }
}
function isChines(source) {
    var regex = /^[\u4E00-\u9FA5]+$/;
    return regex.test(source);
}

function isCardNo(num) {
    if (isNaN(num)) {
        alert("输入的身份证号不是数字！");
        return false;
    }
    var len = num.length;
    if (len < 15 || len > 18) {
        alert("输入的身份证号码长度不正确定！应为15位或18位");
        return false;
    }
    var re15 = /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$/;
    var re18 = /^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{4}$/;
    var res = (re15.test(num) || re18.test(num));
    if (res == false) {
        alert("输入的身份证号格式不正确！");
        return false;
    }
    return res;
}

function checkTime(timevale) {
    var regex = /^(([0-1][0-9])|([2][0-4]))(\:)[0-5][0-9](\:)[0-5][0-9]$/g;
    var b = regex.test(timevale);
    return b;
}

function checkLetOrNum(letVale) {
    var regex = /^([a-zA-Z_]{1})([\w]*)$/g;
    var b = regex.test(letVale);
    return b;
}

function interceptStr(source, index) {
    var temp = source.charAt(index);
    return parseInt(temp);
}

function isEmpty(source) {
    if(source === null || source === undefined || source === ''){
        return true
    }else{
        return false
    }
    
    // var str = source.toString().replace(/(^\s*)|(\s*$)/g, "");
    // if (str == "" || str.toLowerCase() == "null" || str.length <= 0) {
    //     return true;
    // } else {
    //     return false;
    // }
}

function isNotEmpty(source) {
    return !isEmpty(source)
}

function isNotEmptyWithSomeKey(obj, keys){
    for(var i=0;i<keys.length;i++){
        if(isEmpty(obj[keys[i]])){
            return false
        }
    }
    return true
}

/*
 *判断包含关系
 *string:原始字符串
 *substr:子字符串
 *isIgnoreCase:忽略大小写
 */


function jsContains(string, substr, isIgnoreCase) {
    if (isIgnoreCase) {
        string = string.toLowerCase();
        substr = substr.toLowerCase();
    }
    var startChar = substr.substring(0, 1);
    var strLen = substr.length;
    for (var j = 0; j < string.length - strLen + 1; j++) {
        if (string.charAt(j) == startChar)//如果匹配起始字符,开始查找
        {
            if (string.substring(j, j + strLen) == substr)//如果从j开始的字符与str匹配，那ok
            {
                return true;
            }
        }
    }
    return false;
}
function showCustToast(msg, icon = 'none',){
    uni.showToast({
        title: msg,
        icon: icon,
    })
}
function isInteger(str){
    return /^\d+$/.test(str)
}
function isIntegerGtZero(str){
    return /^\d+$/.test(str) && parseInt(str) > 0
}

function isIntegerLtValue(str, maxValue){
    console.log('数字不能大于某个数字', str, maxValue)
    return /^\d+$/.test(str) && parseInt(str) < maxValue
}

export default {
    isTelephone,
    isMobilePhone,
    isEmail,
    isChines,
    isCardNo,
    checkTime,
    checkLetOrNum,
    jsContains,
    isEmpty,
    isNotEmpty,
    interceptStr,
    isNotEmptyWithSomeKey,
    showCustToast,
    isInteger,
    isIntegerGtZero,
    isIntegerLtValue
}